﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;
using System.Text;

namespace Jb.Web.Controllers
{
    public class FeedbackController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public FeedbackController()
		{
		}

		public FeedbackController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index(String Sent)
        {
            FeedbackViewModel model = new FeedbackViewModel();
            if (!Sent.IsNullOrEmpty())
                ViewData[AppConstants.ViewDataKeys.SUB_MODEL] = String.Compare(Sent, "True", true) == 0 ? true : false;
            return View(model);
        }
        

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(FeedbackViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                StringBuilder template = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
                StringBuilder message = new StringBuilder(10);

                viewModel.Subject = String.Format("Feedback >> {0}", viewModel.Subject.HtmlEncode());
                if (!viewModel.Name.IsNullOrEmpty())
                    viewModel.Email = String.Format("\"{0}\"<{1}>", viewModel.Name.HtmlEncode(), viewModel.Email);
                
                message.Append("Dear Admin,<br />");
                message.AppendFormat("A new Feedback received from {0}<br/>", viewModel.Name.IsNullOrEmpty() ? viewModel.Email : viewModel.Name.HtmlEncode());
                message.Append("<div style='margin-top:10px; border-bottom:#efefef 1px solid;'><b>Check the Message Below:</b></div>");
                message.Append(WebUtil.FormatData(viewModel.Message));
                template.Replace(AppConstants.ETConstants.MESSAGE, message.ToString());
                template.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());


                if (WebUtil.SendMail(viewModel.Email, ConfigReader.AdminEmail, String.Empty, String.Empty, viewModel.Subject, template.ToString()))
                    return RedirectToAction("Index", new { @Sent = "True" });
                else
                    return RedirectToAction("Index", new { @Sent = "False" });
            }
            return View(viewModel);
        }		
		#endregion Action Methods
		
		#region Helper Methods

		#endregion			
    }
}


